package com.ttfeed.server.api.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ttfeed.server.api.domain.entity.RecentFeedRecord;
import com.ttfeed.server.api.domain.response.RecentFeedLatestRecordResp;
import com.ttfeed.server.api.mapper.RecentFeedRecordMapper;
import com.ttfeed.server.common.domain.base.ListReq;
import com.ttfeed.server.common.domain.base.PageResult;
import com.ttfeed.server.common.enums.DelFlagEnum;
import com.ttfeed.server.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class RecentFeedRecordService extends ServiceImpl<RecentFeedRecordMapper, RecentFeedRecord> {

    public PageResult<RecentFeedRecord> getList(ListReq listReq) {
        int page = listReq.getPage();
        int size = listReq.getSize();
        Page<RecentFeedRecord> pageEntity = PageHelper.startPage(page, size);
        List<RecentFeedRecord> listResult = this.list(
                Wrappers.lambdaQuery(RecentFeedRecord.class)
                        .eq(RecentFeedRecord::getDelFlag, DelFlagEnum.NOTDEL.getFlag())
                        .orderByDesc(RecentFeedRecord::getUpdateTime));
        PageResult<RecentFeedRecord> pageResult = new PageResult<>(pageEntity.getTotal(), page, size);
        pageResult.setContent(listResult);
        return pageResult;
    }

    public String getType(Integer typeId) {
        switch (typeId) {
            case 1:
                return "嘘嘘 + 便便";
            case 2:
                return "嘘嘘";
            case 3:
                return "便便";
            case 4:
                return "奶瓶喂养";
        }
        return "";
    }

    public String formatLatestRespTime(Date updateTime) {
        if(true) {

        }
        return "";
    }

    public RecentFeedLatestRecordResp getLatestRecordByTypeIdList(Integer type, List<Integer> typeIdList) {
        RecentFeedRecord recentFeedRecord = this.getOne(Wrappers.lambdaQuery(RecentFeedRecord.class)
                .eq(RecentFeedRecord::getDelFlag, DelFlagEnum.NOTDEL.getFlag())
                .in(RecentFeedRecord::getTypeId, typeIdList)
                .orderByDesc(RecentFeedRecord::getUpdateTime)
                .last("limit 1"));
        RecentFeedLatestRecordResp resp = new RecentFeedLatestRecordResp();
        if(!Objects.isNull(recentFeedRecord)) {
            resp.setId(recentFeedRecord.getId()).setTypeId(recentFeedRecord.getTypeId()).setType(this.getType(resp.getTypeId()));
            String time = this.formatTime(recentFeedRecord.getUpdateTime());
            resp.setTime(time);
            if(type == 0) {
                int diffDay = DateUtils.getDiffDay(recentFeedRecord.getUpdateTime(), new Date());
                resp.setSize(diffDay + "天前");
            }
            if(type == 1) {
                resp.setSize(recentFeedRecord.getSize() + "ml");
            }
        }
        return resp;
    }

    /**
     * 本周和上周的写上星期，其他写日期
     * @param date
     * @return
     */
    private String formatTime(Date date) {
        String time = "";
        Date beginOfWeek = DateUtil.beginOfWeek(new Date());
        Date beginOfLastWeek = DateUtil.offsetWeek(DateUtil.beginOfWeek(new Date()), -1);
        Date now = DateUtil.beginOfDay(new Date());
        if(date.after(beginOfWeek)) {
            // 当天的话需要写今天
            if(now.getTime() == DateUtil.beginOfDay(date).getTime()) {
               return "今天";
            }else {
                time += "本";
            }
        }else if(date.after(beginOfLastWeek) && date.before(beginOfWeek)) {
            time += "上";
        }else {
            return DateUtil.format(date, "yyyy/MM/dd");
        }
        time += DateUtils.getNumberOfWeek(date) + DateUtils.getHourAndMinute(date);
        return time;
    }
}
